<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>上传数据</title>
    <script src="../static/js/jQuery3.7.1.js"></script>
</head>

<body>
<div class="title">
    <form>
        <input type="file" class="input_text" id="file"/>
        <input type="button" value="上传" class="submitBtn" id="upload"/>
    </form>
</div>
</body>
<script>
    function upfile() {
        let formData = new FormData();
        formData.append("file", document.getElementById('file').files[0]);
        $.ajax({
            url: '/demo/replace/upload',
            type: 'POST',
            data: formData, // 上传formdata封装的数据包
            dataType: 'JSON',
            cache: false, // 不缓存
            processData: false, // jQuery不要去处理发送的数据
            contentType: false, // jQuery不要去设置Content-Type请求头
            success: function(data) { // 成功回调
                console.log(data);
                if(data.errCode!==0) {
                    alert("错误："+data.errMsg);
                } else {
                    alert("上传成功~");
                }
            }
        });
    }

    $(function () {
        $("#upload").on('click', upfile);
    });
</script>
<style>
    span{
        float: left;
        text-align: right;
    }
    .title{
        margin: 0 auto;
    }
    .input_text {
        width: 140px;
        height: 22px;
        line-height: 22px;
        border: 1px solid #a5c3e0;
        border-top: 1px solid #89accd;
        border-right: 1px solid #89accd;
        padding: 0 3px;
        border-radius: 3px;
        box-shadow: 1px 2px 2px #e5eaee inset;
        font-size: 12px;
        /*margin-left: 90px;*/
    }
    .submitBtn{
        margin-top: 10px;
        background-color: #2283C5 !important;
        border: 4px solid #2283C5;
        color: #FFFFFF !important;
        border-radius: 3px;
        text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25) !important;
        transition: all 0.15s ease 0s;
        vertical-align: middle;
    }
    .submitBtn:hover{
        background-color: #045E9F !important;
        border: 4px solid #045E9F;
    }
    .submitBtn:active{
        left:1px;
        top:1px;
    }
</style>
</html>